<template>
  <div class="daily-container">
    <!--表单-->
    <el-form :inline="true" class="demo-form-inline" ref="dailyForm">

      <el-form-item label="数据类型：">
        <el-select v-model="searchObj.type" placeholder="请选择数据类型">
          <el-option label="课程发布数" value="course"/>
          <el-option label="课程咨询数" value="consult"/>
          <el-option label="订单成交数" value="order"/>
          <el-option label="总收入数" value="money"/>
        </el-select>
      </el-form-item>

      <el-form-item prop="date">
        <el-date-picker
          v-model="searchObj.date"
          type="daterange"
          align="right"
          unlink-panels
          value-format="yyyy-MM-dd"
          range-separator="至 "
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>

      <el-button
        :disabled="btnDisabled"
        type="primary"
        icon="el-icon-search"
        @click="showChart()">查询</el-button>
    </el-form>

    <div class="chart-container">
      <h3 v-if="tipsFlag">Tips：请选择开始日期、结束日期和类型进行数据查询</h3>
      <div id="chart" class="chart" style="height:500px;width:100%" />
    </div>
  </div>
</template>

<script>
  import { getDaily } from '@/api/daily'
  import  'echarts/theme/macarons.js'
  import echarts from 'echarts'
  import { mapGetters } from 'vuex'

  export default {
    data() {
      return {
        pickerOptions: {
          //不可选择当天以后日期
          disabledDate(time) {
            return time.getTime() > Date.now() - 8.64e6;
          },

          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          },]
        },
        searchObj: {
          type: 'course',
          date: ''
        },
        btnDisabled: false,
        chart: null,
        title: '',
        name: '数量',
        xData: [],
        yData: [],
        tipsFlag: true,
      }
    },

    methods: {
      showChart() {
        this.initChartData()
      },

      // 准备图表数据
      initChartData() {
        if (this.searchObj.date.length === 2 ){

          this.tipsFlag = false
          this.xData = []
          this.yData = []

          getDaily(this.teacherId,this.searchObj.date[0],this.searchObj.date[1],this.searchObj.type).then(result => {
            let { code, data } = result
            if (code === 20000){

              data.item.forEach((item)=> {
                // 数据
                this.yData.push(item.count)
                // 横轴时间
                this.xData.push(item.date)
              })

              // 当前统计类别
              switch (this.searchObj.type) {
                case 'course':
                  this.title = '课程发布数'
                  this.name = '数量'
                  break
                case 'consult':
                  this.title = '课程咨询数'
                  this.name = '数量'
                  break
                case 'order':
                  this.title = '订单成交数'
                  this.name = '数量'
                  break
                case 'money':
                  this.title = '总收入数'
                  this.name = '金额'
                  break
              }

              this.setChart()
            }
          })
        }
        //没有选择日期
        else {
          this.$message.error("请选择查询日期！")
        }
      },

      // 设置图标参数
      setChart() {
        // 基于准备好的dom，初始化echarts实例
        this.chart = echarts.init(document.getElementById('chart'),'macarons')

        // 指定图表的配置项和数据
        let option = {
          // x轴是类目轴（离散数据）,必须通过data设置类目数据
          xAxis: {
            type: 'category',
            data: this.xData
          },
          // y轴是数据轴（连续数据）
          yAxis: {
            type: 'value'
          },
          title: {
            text: this.title
          },
          tooltip: {
            trigger: 'axis'
          },
          // 系列列表，每个系列通过 type 决定自己的图表类型
          series: [{
            name: this.name,
            // 系列中的数据内容数组
            data: this.yData,
            // 折线图
            type: 'line',
            itemStyle : { normal: {label : {show: true}}},
          }],
          legend: {
            data:[this.name]
          },
          dataZoom: [{
            show: true,
            height: 30,
            xAxisIndex: [
              0
            ],
            bottom: 30,
            start: 0,
            end: 100,
            handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
            handleSize: '110%',
            handleStyle: {
              color: '#d3dee5'

            },
            textStyle: {
              color: '#fff'
            },
            borderColor: '#90979c'
          },
            {
              type: 'inside',
              show: true,
              height: 15,
              start: 1,
              end: 35
            }]
        }

        this.chart.setOption(option)
      },

      mounted() {
        this.showChart()
      },
    },

    computed: {
      ...mapGetters({
        'teacherId': 'id'
      })
    }
  }
</script>

<style>
  .daily-container{
    margin-top: 20px;
    margin-left: 15px;
  }
</style>
